package quests.Q420_LittleWings;

import com.l2device.gameserver.utils.Rnd;
import com.l2device.gameserver.model.actor.L2Npc;
import com.l2device.gameserver.model.actor.instance.L2PcInstance;
import com.l2device.gameserver.model.quest.Quest;
import com.l2device.gameserver.model.quest.QuestState;

public class Q420_LittleWings extends Quest
{
	private static final String qn = "Q420_LittleWings";
	
	// NPCs
    private static final int COOPER = 30829;
    private static final int CRONOS = 30610;
    private static final int BYRON = 30711;
    private static final int MARIA = 30608;
    private static final int MIMYU = 30747;
    private static final int EXARION = 30748;
    private static final int ZMOV = 30749;
    private static final int KALIBRAN = 30750;
    private static final int SUZET = 30751;
    private static final int SHAMHAI = 30752;
    private static final int ENCHANTED_VALEY_FIRST = 20589;
    private static final int ENCHANTED_VALEY_LAST = 20599;
    private static final int TOAD_LORD = 20231;
    private static final int MARSH_SPIDER = 20233;
    private static final int LETO_LIZARDMAN_WARRIOR = 20580;
    private static final int ROAD_SCAVENGER = 20551;
    private static final int BREKA_ORC_OVERLORD = 20270;
    private static final int DEAD_SEEKER = 20202;
    
    // Items
    private static final short COAL = 1870;
    private static final short CHARCOAL = 1871;
    private static final short SILVER_NUGGET = 1873;
    private static final short STONE_OF_PURITY = 1875;
    private static final short GEMSTONE_D = 2130;
    private static final short GEMSTONE_C = 2131;
    private static final short DRAGONFLUTE_OF_WIND = 3500;
    private static final short DRAGONFLUTE_OF_TWILIGHT = 3502;
    private static final short HATCHLINGS_SOFT_LEATHER = 3912;
    private static final short HATCHLINGS_MITHRIL_COAT = 3918;
    private static final short FOOD_FOR_HATCHLING = 4038;
    private static final short FAIRY_DUST = 3499;
    private static final short FAIRY_STONE = 3816;
    private static final short DELUXE_FAIRY_STONE = 3817;
    private static final short FAIRY_STONE_LIST = 3818;
    private static final short DELUXE_FAIRY_STONE_LIST = 3819;
    private static final short TOAD_LORD_BACK_SKIN = 3820;
    private static final short JUICE_OF_MONKSHOOD = 3821;
    private static final short SCALE_OF_DRAKE_EXARION = 3822;
    private static final short SCALE_OF_DRAKE_ZWOV = 3824;
    private static final short SCALE_OF_DRAKE_KALIBRAN = 3826;
    private static final short SCALE_OF_WYVERN_SUZET = 3828;
    private static final short SCALE_OF_WYVERN_SHAMHAI = 3830;
    private static final short EGG_OF_DRAKE_EXARION = 3823;
    private static final short EGG_OF_DRAKE_ZMOV = 3825;
    private static final short EGG_OF_DRAKE_KALIBRAN = 3827;
    private static final short EGG_OF_WYVERN_SUZET = 3829;
    private static final short EGG_OF_WYVERN_SHAMHAI = 3831;
    
    // Chance
    private static final int TOAD_LORD_BACK_SKIN_CHANCE = 30;
    private static final int EGG_CHANCE = 50;
    private static final int PET_ARMOR_CHANCE = 35;
    
    private static final short[][] FAIRY_STONE_ITEMS =
    {
        { COAL, 10 },
        { CHARCOAL, 10 },
        { GEMSTONE_D, 1 },
        { SILVER_NUGGET, 3 },
        { TOAD_LORD_BACK_SKIN, 10 }
    };
    
    private static short[][] DELUXE_FAIRY_STONE_ITEMS =
    {
        { COAL, 10 },
        { CHARCOAL, 10 },
        { GEMSTONE_C, 1 },
        { STONE_OF_PURITY, 1 },
        { SILVER_NUGGET, 5 },
        { TOAD_LORD_BACK_SKIN, 20 }
    };
    
    private static final int[][] wyrms =
    {
    	{ LETO_LIZARDMAN_WARRIOR, EXARION, SCALE_OF_DRAKE_EXARION, EGG_OF_DRAKE_EXARION },
        { MARSH_SPIDER, ZMOV, SCALE_OF_DRAKE_ZWOV, EGG_OF_DRAKE_ZMOV },
        { ROAD_SCAVENGER, KALIBRAN, SCALE_OF_DRAKE_KALIBRAN, EGG_OF_DRAKE_KALIBRAN },
        { BREKA_ORC_OVERLORD, SUZET, SCALE_OF_WYVERN_SUZET, EGG_OF_WYVERN_SUZET },
        { DEAD_SEEKER, SHAMHAI, SCALE_OF_WYVERN_SHAMHAI, EGG_OF_WYVERN_SHAMHAI }
    };

    public Q420_LittleWings()
    {
        super(420, qn, "Little Wings");
        
        questItemIds = new int[]
        {
			FAIRY_DUST,
			FAIRY_STONE,
			DELUXE_FAIRY_STONE,
			FAIRY_STONE_LIST,
			DELUXE_FAIRY_STONE_LIST,
			TOAD_LORD_BACK_SKIN,
			JUICE_OF_MONKSHOOD,
			SCALE_OF_DRAKE_EXARION,
			SCALE_OF_DRAKE_ZWOV,
			SCALE_OF_DRAKE_KALIBRAN,
			SCALE_OF_WYVERN_SUZET,
			SCALE_OF_WYVERN_SHAMHAI,
			EGG_OF_DRAKE_EXARION,
			EGG_OF_DRAKE_ZMOV,
			EGG_OF_DRAKE_KALIBRAN,
			EGG_OF_WYVERN_SUZET,
			EGG_OF_WYVERN_SHAMHAI
		};
        
        addStartNpc(COOPER);
		addTalkId(COOPER, CRONOS, MIMYU, BYRON, MARIA);
        
        addKillId(TOAD_LORD);
        for (int Enchanted_Valey_id = ENCHANTED_VALEY_FIRST; Enchanted_Valey_id <= ENCHANTED_VALEY_LAST; Enchanted_Valey_id++)
        {
            addKillId(Enchanted_Valey_id);
        }
        for (int[] wyrm : wyrms)
        {
            addTalkId(wyrm[1]);
            addKillId(wyrm[0]);
        }
    }

    @Override
    public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
    {
		QuestState st = player.getQuestState(qn);
        int cond = st.getInt("cond");
        if (event.equalsIgnoreCase("30829-02.htm") && st.getState() == Quest.STATE_CREATED)
        {
            st.setState(Quest.STATE_STARTED);
            st.set("cond", "1");
            st.playSound(QuestState.SOUND_ACCEPT);
        }
        else if (event.startsWith("30610-") && st.getState() == Quest.STATE_STARTED && cond == 1)
        {
            st.set("cond", "2");
            st.takeItems(FAIRY_STONE, -1);
            st.takeItems(DELUXE_FAIRY_STONE, -1);
            st.takeItems(FAIRY_STONE_LIST, -1);
            st.takeItems(DELUXE_FAIRY_STONE_LIST, -1);
            st.giveItems(event.equalsIgnoreCase("30610-05.htm") || event.equalsIgnoreCase("30610-12.htm") ? FAIRY_STONE_LIST : DELUXE_FAIRY_STONE_LIST, 1);
            st.playSound(QuestState.SOUND_MIDDLE);
        }
        else if (event.equalsIgnoreCase("30608-03.htm") && st.getState() == Quest.STATE_STARTED && cond == 2 && st.getQuestItemsCount(FAIRY_STONE_LIST) > 0)
        {
            if (CheckFairyStoneItems(st, FAIRY_STONE_ITEMS))
            {
                st.set("cond", "3");
                TakeFairyStoneItems(st, FAIRY_STONE_ITEMS);
                st.giveItems(FAIRY_STONE, 1);
                st.playSound(QuestState.SOUND_MIDDLE);
            }
            else
            {
                return "30608-01.htm";
            }
        }
        else if (event.equalsIgnoreCase("30608-03a.htm") && st.getState() == Quest.STATE_STARTED && cond == 2 && st.getQuestItemsCount(DELUXE_FAIRY_STONE_LIST) > 0)
        {
            if (CheckFairyStoneItems(st, DELUXE_FAIRY_STONE_ITEMS))
            {
                st.set("cond", "3");
                TakeFairyStoneItems(st, DELUXE_FAIRY_STONE_ITEMS);
                st.giveItems(DELUXE_FAIRY_STONE, 1);
                st.playSound(QuestState.SOUND_MIDDLE);
            }
            else
            {
                return "30608-01a.htm";
            }
        }
        else if (event.equalsIgnoreCase("30711-03.htm") && st.getState() == Quest.STATE_STARTED && cond == 3 && st.getQuestItemsCount(FAIRY_STONE) + st.getQuestItemsCount(DELUXE_FAIRY_STONE) > 0)
        {
            st.set("cond", "4");
            st.playSound(QuestState.SOUND_MIDDLE);
            if (st.getQuestItemsCount(DELUXE_FAIRY_STONE) > 0)
            {
                return st.getInt("broken") == 1 ? "30711-04a.htm" : "30711-03a.htm";
            }
            if (st.getInt("broken") == 1)
            {
                return "30711-04.htm";
            }
        }
        else if (event.equalsIgnoreCase("30747-02.htm") && st.getState() == Quest.STATE_STARTED && cond == 4 && st.getQuestItemsCount(FAIRY_STONE) > 0)
        {
            st.takeItems(FAIRY_STONE, -1);
            st.set("takedStone", "1");
        }
        else if (event.equalsIgnoreCase("30747-02a.htm") && st.getState() == Quest.STATE_STARTED && cond == 4 && st.getQuestItemsCount(DELUXE_FAIRY_STONE) > 0)
        {
            st.takeItems(DELUXE_FAIRY_STONE, -1);
            st.set("takedStone", "2");
            st.giveItems(FAIRY_DUST, 1);
            st.playSound(QuestState.SOUND_ITEMGET);
        }
        else if (event.equalsIgnoreCase("30747-04.htm") && st.getState() == Quest.STATE_STARTED && cond == 4 && st.getInt("takedStone") > 0)
        {
            st.set("cond", "5");
            st.unset("takedStone");
            st.giveItems(JUICE_OF_MONKSHOOD, 1);
            st.playSound(QuestState.SOUND_ITEMGET);
        }
        else if (event.equalsIgnoreCase("30748-02.htm") && cond == 5 && st.getState() == Quest.STATE_STARTED && st.getQuestItemsCount(JUICE_OF_MONKSHOOD) > 0)
        {
            st.set("cond", "6");
            st.takeItems(JUICE_OF_MONKSHOOD, -1);
            st.giveItems(3822, 1);
            st.playSound(QuestState.SOUND_ITEMGET);
        }
        else if (event.equalsIgnoreCase("30749-02.htm") && cond == 5 && st.getState() == Quest.STATE_STARTED && st.getQuestItemsCount(JUICE_OF_MONKSHOOD) > 0)
        {
            st.set("cond", "6");
            st.takeItems(JUICE_OF_MONKSHOOD, -1);
            st.giveItems(3824, 1);
            st.playSound(QuestState.SOUND_ITEMGET);
        }
        else if (event.equalsIgnoreCase("30750-02.htm") && cond == 5 && st.getState() == Quest.STATE_STARTED && st.getQuestItemsCount(JUICE_OF_MONKSHOOD) > 0)
        {
            st.set("cond", "6");
            st.takeItems(JUICE_OF_MONKSHOOD, -1);
            st.giveItems(3826, 1);
            st.playSound(QuestState.SOUND_ITEMGET);
        }
        else if (event.equalsIgnoreCase("30751-02.htm") && cond == 5 && st.getState() == Quest.STATE_STARTED && st.getQuestItemsCount(JUICE_OF_MONKSHOOD) > 0)
        {
            st.set("cond", "6");
            st.takeItems(JUICE_OF_MONKSHOOD, -1);
            st.giveItems(3828, 1);
            st.playSound(QuestState.SOUND_ITEMGET);
        }
        else if (event.equalsIgnoreCase("30752-02.htm") && cond == 5 && st.getState() == Quest.STATE_STARTED && st.getQuestItemsCount(JUICE_OF_MONKSHOOD) > 0)
        {
            st.set("cond", "6");
            st.takeItems(JUICE_OF_MONKSHOOD, -1);
            st.giveItems(3830, 1);
            st.playSound(QuestState.SOUND_ITEMGET);
        }
        else if (event.equalsIgnoreCase("30747-09.htm") && st.getState() == Quest.STATE_STARTED && cond == 7)
        {
            int egg_id = 0;
            for (int[] wyrm : wyrms)
            {
                if (st.getQuestItemsCount(wyrm[2]) == 0 && st.getQuestItemsCount(wyrm[3]) >= 1)
                {
                    egg_id = wyrm[3];
                    break;
                }
            }
            if (egg_id == 0)
            {
                return "noquest";
            }
            st.takeItems(egg_id, -1);
            st.giveItems(Rnd.get(DRAGONFLUTE_OF_WIND, DRAGONFLUTE_OF_TWILIGHT), 1);
            if (st.getQuestItemsCount(FAIRY_DUST) > 0)
            {
                st.playSound(QuestState.SOUND_MIDDLE);
                return "30747-09a.htm";
            }
            st.playSound(QuestState.SOUND_FINISH);
            st.exitQuest(true);
        }
        else if (event.equalsIgnoreCase("30747-10.htm") && st.getState() == Quest.STATE_STARTED && cond == 7)
        {
            st.playSound(QuestState.SOUND_FINISH);
            st.exitQuest(true);
        }
        else if (event.equalsIgnoreCase("30747-11.htm") && st.getState() == Quest.STATE_STARTED && cond == 7)
        {
            st.playSound(QuestState.SOUND_FINISH);
            st.exitQuest(true);
            if (st.getQuestItemsCount(FAIRY_DUST) == 0)
            {
                return "30747-10.htm";
            }
            st.takeItems(FAIRY_DUST, -1);
            if (Rnd.get(PET_ARMOR_CHANCE) > FAIRY_DUST)
            {
                int armor_id = HATCHLINGS_SOFT_LEATHER + Rnd.get(2);
                if (armor_id > HATCHLINGS_MITHRIL_COAT)
                {
                    armor_id = HATCHLINGS_MITHRIL_COAT;
                }
                st.giveItems(armor_id, 1);
            }
            else
            {
                st.giveItems(FOOD_FOR_HATCHLING, 20);
            }
        }
        return event;
    }

    @Override
    public String onTalk(L2Npc npc, L2PcInstance player)
    {
		QuestState st = player.getQuestState(qn);
        int _state = st.getState();
        int npcId = npc.getNpcId();
        if (_state == Quest.STATE_CREATED)
        {
            if (npcId != COOPER)
                return getNoQuestMsg();
            if (st.getPlayer().getLevel() < 35)
            {
                st.exitQuest(true);
                return "30829-00.htm";
            }
            st.set("cond", "0");
            return "30829-01.htm";
        }
        if (_state != Quest.STATE_STARTED)
            return getNoQuestMsg();
        int cond = st.getInt("cond");
        int broken = st.getInt("broken");
        if (npcId == COOPER)
        {
            if (cond == 1)
                return "30829-02.htm";
            return "30829-03.htm";
        }
        if (npcId == CRONOS)
        {
            if (cond == 1)
                return broken == 1 ? "30610-10.htm" : "30610-01.htm";
            if (cond == 2)
                return "30610-07.htm";
            if (cond == 3)
                return broken == 1 ? "30610-14.htm" : "30610-08.htm";
            if (cond == 4)
                return "30610-09.htm";
            if (cond > 4)
                return "30610-11.htm";
        }
        if (npcId == MARIA)
        {
            if (cond == 2)
            {
                if (st.getQuestItemsCount(DELUXE_FAIRY_STONE_LIST) > 0)
                    return CheckFairyStoneItems(st, DELUXE_FAIRY_STONE_ITEMS) ? "30608-02a.htm" : "30608-01a.htm";
                if (st.getQuestItemsCount(FAIRY_STONE_LIST) > 0)
                    return CheckFairyStoneItems(st, FAIRY_STONE_ITEMS) ? "30608-02.htm" : "30608-01.htm";
            }
            else if (cond > 2)
                return "30608-04.htm";
        }
        if (npcId == BYRON)
        {
            if (cond == 1 && broken == 1)
                return "30711-06.htm";
            if (cond == 2 && broken == 1)
                return "30711-07.htm";
            if (cond == 3 && st.getQuestItemsCount(FAIRY_STONE) + st.getQuestItemsCount(DELUXE_FAIRY_STONE) > 0)
                return "30711-01.htm";
            if (cond >= 4 && st.getQuestItemsCount(DELUXE_FAIRY_STONE) > 0)
                return "30711-05a.htm";
            if (cond >= 4 && st.getQuestItemsCount(FAIRY_STONE) > 0)
                return "30711-05.htm";
        }
        if (npcId == MIMYU)
        {
            if (cond == 4 && st.getQuestItemsCount(DELUXE_FAIRY_STONE) > 0)
                return "30747-01a.htm";
            if (cond == 4 && st.getQuestItemsCount(FAIRY_STONE) > 0)
                return "30747-01.htm";
            if (cond == 5)
                return "30747-05.htm";
            if (cond == 6)
            {
                for (int[] wyrm : wyrms)
                {
                    if (st.getQuestItemsCount(wyrm[2]) == 0 && st.getQuestItemsCount(wyrm[3]) >= 20)
                        return "30747-07.htm";
                }
                return "30747-06.htm";
            }
            if (cond == 7)
            {
                for (int[] wyrm : wyrms)
                {
                    if (st.getQuestItemsCount(wyrm[2]) == 0 && st.getQuestItemsCount(wyrm[3]) >= 1)
                        return "30747-08.htm";
                }
            }
        }
        if (npcId >= EXARION && npcId <= SHAMHAI)
        {
            if (cond == 5 && st.getQuestItemsCount(JUICE_OF_MONKSHOOD) > 0)
            {
                return String.valueOf(npcId) + "-01.htm";
            }
            if (cond == 6 && st.getQuestItemsCount(getWyrmScale(npcId)) > 0)
            {
                int egg_id = getWyrmEgg(npcId);
                if (st.getQuestItemsCount(egg_id) < 20)
                {
                    return String.valueOf(npcId) + "-03.htm";
                }
                st.takeItems(getWyrmScale(npcId), -1);
                st.takeItems(egg_id, -1);
                st.giveItems(egg_id, 1);
                st.set("cond", "7");
                return String.valueOf(npcId) + "-04.htm";
            }
            if (cond == 7 && st.getQuestItemsCount(getWyrmEgg(npcId)) == 1)
                return String.valueOf(npcId) + "-05.htm";
        }
        return getNoQuestMsg();
    }

    @Override
    public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
    {
		QuestState st = checkPlayerState(player, npc, STATE_STARTED);
        if (st == null)
            return null;

        int npcId = npc.getNpcId();
        int cond = st.getInt("cond");
        if (cond == 2 && npcId == TOAD_LORD)
        {
            int needed_skins = getNeededSkins(st);
            if (st.getQuestItemsCount(TOAD_LORD_BACK_SKIN) < needed_skins && Rnd.get(TOAD_LORD_BACK_SKIN_CHANCE) > TOAD_LORD_BACK_SKIN_CHANCE)
            {
                st.giveItems(TOAD_LORD_BACK_SKIN, 1);
                st.playSound(st.getQuestItemsCount(TOAD_LORD_BACK_SKIN) < needed_skins ? QuestState.SOUND_ITEMGET : QuestState.SOUND_MIDDLE);
            }
            return null;
        }
        if (npcId >= ENCHANTED_VALEY_FIRST && npcId <= ENCHANTED_VALEY_LAST && st.getQuestItemsCount(DELUXE_FAIRY_STONE) > 0)
        {
            st.takeItems(DELUXE_FAIRY_STONE, 1);
            st.set("broken", "1");
            st.set("cond", "1");
            return "You lost fairy stone deluxe!";
        }
        if (cond == 6)
        {
            int wyrm_id = isWyrmStoler(npcId);
            if (wyrm_id > 0 && st.getQuestItemsCount(getWyrmScale(wyrm_id)) > 0 && st.getQuestItemsCount(getWyrmEgg(wyrm_id)) < 20 && Rnd.get(EGG_CHANCE) > EGG_CHANCE)
            {
                st.giveItems(getWyrmEgg(wyrm_id), 1);
                st.playSound(st.getQuestItemsCount(getWyrmEgg(wyrm_id)) < 20 ? QuestState.SOUND_ITEMGET : QuestState.SOUND_MIDDLE);
            }
        }
        return null;
    }

    private static int getWyrmScale(int npc_id)
    {
        for (int[] wyrm : wyrms)
        {
            if (npc_id == wyrm[1])
            {
                return wyrm[2];
            }
        }
        return 0;
    }

    private static int getWyrmEgg(int npc_id)
    {
        for (int[] wyrm : wyrms)
        {
            if (npc_id == wyrm[1])
            {
                return wyrm[3];
            }
        }
        return 0;
    }

    private static int isWyrmStoler(int npc_id)
    {
        for (int[] wyrm : wyrms)
        {
            if (npc_id == wyrm[0])
            {
                return wyrm[1];
            }
        }
        return 0;
    }

    public static int getNeededSkins(QuestState st)
    {
        if (st.getQuestItemsCount(DELUXE_FAIRY_STONE_LIST) > 0)
        {
            return 20;
        }
        if (st.getQuestItemsCount(FAIRY_STONE_LIST) > 0)
        {
            return 10;
        }
        return -1;
    }

    public static boolean CheckFairyStoneItems(QuestState st, short[][] item_list)
    {
        for (short[] _item : item_list)
        {
            if (st.getQuestItemsCount(_item[0]) < _item[1])
            {
                return false;
            }
        }
        return true;
    }

    public static void TakeFairyStoneItems(QuestState st, short[][] item_list)
    {
        for (short[] _item : item_list)
        {
            st.takeItems(_item[0], _item[1]);
        }
    }

	public static void main(String[] args)
	{
		new Q420_LittleWings();
	}
}
